﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using DataProcess.Core.DataValidation;
using Newtonsoft.Json;
using SqlSugar;

namespace DataProcess.Core.Model
{
    public class SystemSetting
    {
        public SystemSetting()
        {
            ID = Guid.NewGuid().ToString();
            AnalysisMappingList = new List<AnalysisItemMapping>();
        }

        [Key]
        [Display(AutoGenerateField = false)]
        public string ID { get; set; }

        [Display(Name = "开机启动")]
        public bool AutoRun { get; set; }

        /// <summary>
        /// 数据字典Endpoint
        /// </summary>
        [Display(Name = "数据字典Endpoint")]
        public string EndpointDataDictionary { get; set; }

        /// <summary>
        /// 测量项Endpoint
        /// </summary>
        [Display(Name = "测量项目Endpoint")]
        public string EndpointAnalysisItem { get; set; }

        /// <summary>
        /// 访问授权码
        /// </summary>
        [Display(Name = "访问授权码")]
        public string Author { get; set; }

        /// <summary>
        /// 工厂编码
        /// </summary>
        [Display(Name = "工厂编码")]
        [Required]
        public string TargetCode { get; set; }

        /// <summary>
        /// 工厂名称
        /// </summary>
        [Display(Name = "工厂名称")]
        [Required]
        public string TargetName { get; set; }

        [Display(AutoGenerateField = false)]
        [JsonIgnore]
        public string AnalysisMapping
        {
            get
            {
                if (AnalysisMappingList == null) return string.Empty;
                return JsonConvert.SerializeObject(AnalysisMappingList);
            }
            set
            {
                try
                {
                    var result = JsonConvert.DeserializeObject<List<AnalysisItemMapping>>(value);
                    if (result == null)
                    {
                        AnalysisMappingList = new List<AnalysisItemMapping>();
                    }
                    else
                    {
                        AnalysisMappingList = result;
                    }
                }
                catch
                {
                    AnalysisMappingList = new List<AnalysisItemMapping>();
                }
            }
        }

        /// <summary>
        /// 检测项目映射
        /// </summary>
        [Display(Name = "检测项目映射")]
        [SugarColumn(IsIgnore = true)]
        [CustomValidation(typeof(SystemSettingValidation), nameof(SystemSettingValidation.AnalysisItemsValidation))]
        public List<AnalysisItemMapping> AnalysisMappingList { get; set; }
    }

    public class AnalysisItemMapping
    {
        public AnalysisItemMapping()
        {
            ID = Guid.NewGuid().ToString();
            SourceName = string.Empty;
            TargetName = string.Empty;
        }

        public string ID { get; set; }

        [Display(Name = "源名称")]
        public string SourceName { get; set; }

        [Display(Name = "目标名称")]
        public string TargetName { get; set; }
    }
}
